<?php

namespace App\Http\Controllers\Back;

use App\Http\Controllers\Controller;
use App\Http\Requests\SalesmenRequest;
use App\Model\Operators;
use App\Model\Partner;
use App\Model\Salesmen;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class SalesmenController extends Controller
{

    public function index(Request $request, Partner $partner, Operators $operators, Salesmen $salesmen)
    {
//        $where = [];
//
//        //管理员看见所有业务员
//        //合伙人看见下属所有业务员
//        if ($request->user()->type == 1) {
//            $where['place_partner_id'] = Auth::id();
//        }
//        if ($request->user()->type == 2) {
//            //运营商看见所属业务员
//            $where['operator_id'] = Auth::id();
//        }
//        $salesmens = $salesmen->getPaginate($request, $where);
//        return $this->renderJson($salesmens);
        $model = DB::table('users as u')
            ->select('s.*', 'u.is_enabled')
            ->leftJoin('salesmen as s', 's.user_id', '=', 'u.id')
            ->where('u.is_fase_distribution', '<>', 1)
            ->where('u.is_change_grade', '<>', 1)
            ->where('u.type', 3);
        if (Auth::user()->type == 1){
            $model->where('u.place_partner_id', Auth::id());
        }
        if (Auth::user()->type == 2){
            $model->where('u.operator_id', Auth::id());
        }
        $page = $model->paginate(\request('limit', 10));
        $items = $page->items();
        foreach ($items as &$item) {
            $item->member_num = DB::table('users as u')->leftJoin('user_grade as ug', 'ug.user_id', '=', 'u.id')->leftJoin('vip_fee_config as vf', 'ug.vip_fee_config_id', '=', 'vf.id')->where('u.salesman_id', $item->user_id)->count();
            $item->user_num = DB::table('users')->where('salesman_id', $item->user_id)->count();
        }
        $data = $page->toArray();
        $data['data'] = $items;
        return $this->renderJson($data);
    }

    public function show($id, Request $request, Partner $partner, Operators $operators, Salesmen $salesmen)
    {
        $salesmen = $salesmen->findOrFail($id);
        return $this->renderJson($salesmen);
    }


    public function store(SalesmenRequest $request, User $user, Partner $partner, Operators $operators, Salesmen $salesmen)
    {

        $u = new User;
        if ($u->where('phone','=',$request->input('phone'))->first()){
            return $this->fail('该手机号已经被注册过了');
        }
        //创建登录用户
        $user->account = request('phone');
        $user->phone = request('phone');
        $user->password = bcrypt('123456');
        $user->type = 3;
        $user->is_enabled = 0;
        $operator = $operators->where(['user_id' => $request->user()->id])->first();
        $salesmen->fill($request->all());
        if (!empty($operator)) {
            $salesmen->operator_id = $operator->id;
            $user->operator_id = $operator->user_id;
            if (!empty($operator->place_partner_id)) {
                $salesmen->place_partner_id = $operator->place_partner_id;
                $p = Partner::find($operator->place_partner_id);
                $user->place_partner_id = $p->user_id;
            }
        } else {
            $p = $partner->where(['user_id' => $request->user()->id])->first();
            if (!empty($p)) {
                $salesmen->place_partner_id = $p->id;
                $user->place_partner_id = $p->user_id;
            }
        }

        try {
            DB::beginTransaction();
            $user->save();
            $salesmen->user_id = $user->id;
            $salesmen->save();
            if (!empty($operator)) {
                $operator->increment('salesman_count');
                if (!empty($p)) {
                    $p->increment('operator_count');
                }
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error('添加失败');
        }
        return $this->success();
    }

    public function update(SalesmenRequest $request, $id, Salesmen $salesmen)
    {
        $s = $salesmen->findOrFail($id);
        $s->fill($request->all());
        if ($s->save()) {
            return $this->success();
        }
        return $this->error('修改失败');
    }

    public function destroy($id, Salesmen $salesmen)
    {
        $s = $salesmen->findOrFail($id);
        try {
            $s->delete();
        } catch (\Exception $e) {
            return $this->error('删除失败');
        }
        return $this->success();
    }

    public function stop(Request $request, $id, Salesmen $salesmen)
    {
        $this->validate($request, [
            'status' => 'boolean'
        ]);
        $status = $request->input('status');
        $s = $salesmen->findOrFail($id);
        $s->status = $status;
        $s->user->is_enabled = $status;
        if ($s->save()) {
            return $this->success();
        }
        return $this->error('修改失败');
    }
}
